package com.gruuf.done.web;

import com.gruuf.done.MailService;
import com.gruuf.done.model.Task;

import java.util.Date;

public class UpdateAction extends BaseAction {

    private String key;
    private String name;
    private String description;
    private String taskType;
    private Date created;
    private Date expectedEnd;
    private Date finished;

    private MailService mailService;

    public UpdateAction() {
        mailService = new MailService();
    }

    public String execute() throws Exception {
        if ("".equals(key)) {
            createTask();
        } else {
            updateTask();
        }
        return SUCCESS;
    }

    private void updateTask() {
        Task task = repository.readTask(key);
        task.setName(name);
        task.setDescription(description);
        task.setType(taskType);
        task.setCreated(created);
        task.setExpectedEnd(expectedEnd);
        task.setFinished(finished);
        repository.updateTask(task);
    }

    private void createTask() {
        Task task = new Task(getUserId(), name, description, taskType, created, expectedEnd, finished);
        repository.updateTask(task);
        mailService.sendMail(task);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getExpectedEnd() {
        return expectedEnd;
    }

    public void setExpectedEnd(Date expectedEnd) {
        this.expectedEnd = expectedEnd;
    }

    public Date getFinished() {
        return finished;
    }

    public void setFinished(Date finished) {
        this.finished = finished;
    }

}
